package com.fanh.controller;

import com.fanh.common.api.CommonPage;
import com.fanh.common.api.CommonResult;
import com.fanh.dto.GuideFavoriteResult;
import com.fanh.model.GuideFavorite;
import com.fanh.service.GuideFavoriteService;
import com.fanh.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@Api(tags = "CollectionController", description = "收藏")
@RequestMapping("/")
public class CollectionController {

    @Autowired
    private UserService userService;
    @Autowired
    private GuideFavoriteService guideFavoriteService;

    @ApiOperation("我的收藏")
    @RequestMapping(value = "a/guide/s/coursefav/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult list(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        String userId= userService.getCurrentSysUser().getId();
        List<GuideFavoriteResult> results= guideFavoriteService.coursefavList(userId,pageNo,pageSize);
       return CommonResult.success(CommonPage.restPage(results));
    }
    @ApiOperation("收藏状态")
    @RequestMapping(value = "a/guide/s/coursefav/check", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult check(@ApiParam(value = "courseId", required = true) @RequestParam("courseId")String courseId) {
        String userId= userService.getCurrentSysUser().getId();
        boolean isFav = guideFavoriteService.checkFav(courseId,userId);
        return CommonResult.success(isFav);
    }

    @ApiOperation("取消收藏")
    @RequestMapping(value = "a/guide/s/coursefav/delete", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult delete(@ApiParam(value = "courseId", required = true) @RequestParam("courseId")String courseId) {
        String userId= userService.getCurrentSysUser().getId();
        int count = guideFavoriteService.deleteCoursefav(courseId,userId);
        if(count>0){
            return  CommonResult.success(count);
        }
        return CommonResult.failed("取消收藏失败！");
    }

    @ApiOperation("添加收藏")
    @RequestMapping(value = "a/guide/s/coursefav/save", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult save(GuideFavorite guideFavorite) {
        String userId= userService.getCurrentSysUser().getId();
        guideFavorite.setUserId(userId);
        boolean isFav = guideFavoriteService.checkFav(guideFavorite.getCourseId(),userId);
        if(isFav) {
            return CommonResult.failed("此课程已经收藏");
        }
           int count = guideFavoriteService.insert(guideFavorite);
        if(count>0){
            return  CommonResult.success(count);
        }
        return CommonResult.failed("收藏失败！");

    }
}
